/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.zuq.geocode;

import com.gisgraphy.domain.geoloc.service.geoloc.street.StreetType;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import static javax.xml.stream.XMLStreamConstants.*;

/**
 * Helper class to parse Gisgraphy valid xml rensponses.
 * 
 * @author zuq
 */
class GisgraphyXMLParser {

    private static final XMLInputFactory inputFactory = XMLInputFactory.newInstance();

    /**
     * Parses a String cointaining a valid gisgraphy xml response.
     * 
     * @param xml
     * @return
     */
    public static List<NearStreet> parseString(String xml) {
        XMLStreamReader reader = null;

        List<NearStreet> result = new ArrayList<NearStreet>();

        try {
            reader = inputFactory.createXMLStreamReader(new StringReader(xml));

            int event = 0;
            NearStreet street = new NearStreet();

            while (reader.hasNext()) {
                event = reader.next();


                if (event == START_ELEMENT) {

                    startElement(reader, street);

                } else if (event == END_ELEMENT) {

                    if (reader.getLocalName().equals("result")) {

                        result.add(street);
                        street = new NearStreet();
                    }
                }
            }

        } catch (XMLStreamException ex) {
            ex.printStackTrace();
        } finally {
            closeStream(reader);
        }

        return result;

    }

    /**
     * Helper method to close a XMLStreamReader.
     * 
     * @param reader
     */
    private static void closeStream(XMLStreamReader reader) {
        if (reader != null) {
            try {
                reader.close();
            } catch (XMLStreamException ex) {
                ex.printStackTrace();
            }
        }
    }

    /**
     * Handles starting elements
     * 
     * @param reader
     * @param street
     * @throws XMLStreamException
     */
    private static void startElement(XMLStreamReader reader, NearStreet street)
            throws XMLStreamException {

        String localName = reader.getLocalName();

        if (localName.equals("name")) {

            street.name = reader.getElementText();

        } else if (localName.equals("distance")) {

            street.distance = Double.parseDouble(reader.getElementText());

        } else if (localName.equals("gid")) {

            street.gid = Long.parseLong(reader.getElementText());

        } else if (localName.equals("streetType")) {

            street.streetType = StreetType.getFromString(reader.getElementText());

        } else if (localName.equals("oneWay")) {

            street.oneWay = Boolean.parseBoolean(reader.getElementText());

        } else if (localName.equals("countryCode")) {

            street.countryCode = reader.getElementText();

        } else if (localName.equals("length")) {

            street.length = Double.parseDouble(reader.getElementText());

        } else if (localName.equals("lat")) {

            street.lat = Double.parseDouble(reader.getElementText());

        } else if (localName.equals("lng")) {

            street.lng = Double.parseDouble(reader.getElementText());

        }

    }
}
